﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Class_Entity;
using BussinessObject;

public partial class WebApplication_ADAccounts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToInt32(Session["RoleCode"]) == 2)
        {
            if (!Page.IsPostBack)
            {
                loadGrid();

            }
        }
        else
            Response.Redirect("ADResume.aspx");
    }
    private void loadGrid()
    {
        Users us = new Users();
        BOUsers bus = new BOUsers();

        GridView1.DataSource = bus.Load_Users(us);
        GridView1.DataBind();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Users us = new Users();
        BOUsers bus = new BOUsers();


        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.Cells[2].Text.Equals(Session["Username"].ToString()))
            {
                Response.Write("<script language='javascript'> alert('" + "Cannot delete your own Account ..." + "'); </script>");
            }
            else
            {
                CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
                if (cb != null && cb.Checked)
                {
                    int id = Convert.ToInt32(row.Cells[1].Text);
                    us.ID = id;
                    bus.Delete_Users(us);

                }
            }
        }

        loadGrid();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (GridView1.Rows[e.RowIndex].Cells[2].Text.Equals(Session["Username"].ToString()))
        {
            Response.Write("<script language='javascript'> alert('" + "Cannot delete your own Account ..." + "'); </script>");
        }
        else
        {
            Users us = new Users();
            BOUsers bus = new BOUsers();

            int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);

            us.ID = id;

            bus.Delete_Users(us);

            loadGrid();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Users us = new Users();
        BOUsers bus = new BOUsers();

        us.Username = txtSearch.Text;

        GridView1.DataSource = bus.Find_UserByUsername(us);
        GridView1.DataBind();
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {
        loadGrid();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("ADAddAccounts.aspx");
    }
}